package com.greate.community.controller.Background;

import com.greate.community.common.Result;
import com.greate.community.entity.User;
import com.greate.community.service.UserService;
import com.greate.community.util.HostHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Collections;

/**
 * @Author:xiaohai 2072158919@qq.com 海航源
 * @Date: 2025/3/31  下午6:00
 * @Version 1.0
 * @description
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin(origins = "http://localhost:5666")
public class BackgroundUserInfoController {

    private static final Logger log = LoggerFactory.getLogger(BackgroundUserInfoController.class);
    @Autowired
    private UserService userService;

    @Autowired
    private HostHolder hostHolder;

    private final String Token = "hnczjr";

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Result<Object> getUserInfo(@RequestHeader(value = "Authorization", required = false) String authorization) {
        log.info("authorization:{}", authorization);
        // 处理 Authorization 头部
        if (authorization != null && authorization.startsWith("Bearer ")) {
            // 提取 Bearer 后面的令牌
            String token = Token;
            // 这里可以根据令牌验证用户身份
            // 假设令牌验证成功，返回用户信息

            Map<String, Object> userData = new HashMap<>();
            userData.put("id", 4);
            userData.put("realName", "Vben");
            userData.put("roles", Collections.singletonList("super"));
            userData.put("username", "vben");
            userData.put("accessToken", token);

            return Result.success(userData);
        }

        // 如果没有提供有效的 Authorization 头部，可以返回错误
        return Result.error("未提供有效的授权信息");
    }


}